package com.huoyun.user.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.huoyun.base.bean.Page;
import com.huoyun.user.bean.Role;
import com.huoyun.user.bean.User;
import com.huoyun.user.dao.RoleDao;
import com.huoyun.user.dao.UserDao;
import com.huoyun.userAdmin.bean.UserDetail;
import com.huoyun.userAdmin.dao.UserDetailDao;

public class UserServiceImpl implements UserService {

	private UserDao userDao;
	private RoleDao roleDao;
	private UserDetailDao userDetailDao;

	@Override
	public long saveUser(User user, int[] groupIds) 
	{
		//保存用户基本信息
		long userId = userDao.saveUser(user);
		
		UserDetail userDetail = new UserDetail();
		userDetail.setUserId(userId);
		userDetail.setRegisterIp(user.getRegisterIp());
		//保存用户的详细信息
		userDetailDao.saveUserDetail(userDetail);
		
		//保存用户与组之间的关联关系
		userDao.userRelationGroup(userId, groupIds);
		
		return userId;
	}

	@Override
	public User getUserByUsername(String username) {
		return userDao.getUserByUsername(username);
	}
	
	
	
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		User user = getUserByUsername(username);
		
		if(user == null)
		{
			throw new UsernameNotFoundException("用户名：" + username + "不存在");
		}
		
		List<Role> roles = roleDao.getRolesByUserId(user.getId());
		
		Set<GrantedAuthority> authoritys = new HashSet<GrantedAuthority>();
		
		if(roles != null)
		{
			for(Role role : roles)
			{
				authoritys.add(new GrantedAuthorityImpl(role.getRoleName()));
			}
		}
		
		user.setAuthorities(authoritys);
		
		user.setAccountNonExpired(true);
		user.setAccountNonLocked(true);
		user.setCredentialsNonExpired(true);
		
		return user;
	}

	@Override
	public User getUserById(long userId) 
	{
		return userDao.getUserById(userId);
	}

	@Override
	public void changePassword(long userId, String newPassword)
	{
		userDao.changePassword(userId, newPassword);
	}
	

	
	@Override
	public void updateCredit(long credit, long id) {
		// TODO Auto-generated method stub
		userDao.updateCredit(credit, id);
	}

	@Override
	public void updateAvatarByUserId(long userId, String avatarUrl) 
	{
		userDao.updateAvatarByUserId(userId, avatarUrl);
	}

	@Override
	public void updateMobilePhoneByUserId(long userId, String mobilePhone) 
	{
		this.userDao.updateMobilePhoneByUserId(userId, mobilePhone);
	}

	@Override
	public void updateEmailByUserId(long userId, String email) 
	{
		this.userDao.updateEmailByUserId(userId, email);
	}

	@Override
	public void updateRealnameByUserId(long userId, String realname) 
	{
		this.userDao.updateRealnameByUserId(userId, realname);
	}

	@Override
	public void updateBankAccountByUserId(int userId, String bankAccount) 
	{
		this.userDao.updateBankAccountByUserId(userId, bankAccount);
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public RoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public UserDetailDao getUserDetailDao() {
		return userDetailDao;
	}

	public void setUserDetailDao(UserDetailDao userDetailDao) {
		this.userDetailDao = userDetailDao;
	}

	//以下是安全密码相关服务
	@Override
	public void changeSecurePassword(long userId, String newSecurePassword) {
		userDao.changeSecurePassword(userId, newSecurePassword);
	}

	@Override
	public void updateSalt(long userId, String salt) {
		// TODO Auto-generated method stub
		this.userDao.upateSalt(userId, salt);
	}
	
}
